package models

import (
	"context"
	"fmt"
	"os"

	"github.com/go-redis/redis/v8"
	"gopkg.in/ini.v1"
)

var redisCtx = context.Background()

var RedisDB *redis.Client

func init() {
	config, iniErr := ini.Load("./conf/app.ini")

	if iniErr != nil {
		fmt.Printf("Fail to read file：%v", iniErr)
		os.Exit(1)
	}

	ip := config.Section("redis").Key("ip").String()
	port := config.Section("redis").Key("port").String()
	password := config.Section("redis").Key("password").String()

	RedisDB = redis.NewClient(&redis.Options{
		Addr:     ip + ":" + port,
		DB:       0,
		Password: password,
	})

	_, err := RedisDB.Ping(redisCtx).Result()
	if err != nil {
		print(err)
	}
}
